package de.lianowar.whereismymoney.fragment;


import de.lianowar.whereismymoney.R;
import de.lianowar.whereismymoney.adapter.AccountListAdapter;
import de.lianowar.whereismymoney.sql.DatabaseHandler;
import de.lianowar.whereismymoney.sql.data.Account;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;


@SuppressLint("ValidFragment")
public class Fra_Setting extends Fragment
{
	private Button creatAccount;
	private DatabaseHandler dbHandler = null;
	private EditText account;
	private Spinner account1;
	private Spinner account2;
	private AccountListAdapter adapter;
	private Button buSave;
	private SharedPreferences prefs;
	public Fra_Setting(){}

	public Fra_Setting(DatabaseHandler pDbHandler)
	{
		
		dbHandler = pDbHandler;

		Log.e("GUI", "Konstruktor");
	}
	
	@Override
	 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		    View view = inflater.inflate(R.layout.la_ta_settings,container, false);
		    prefs = container.getContext().getSharedPreferences("de.lianowar.whereismymoney",Context.MODE_PRIVATE);
		    account1 = (Spinner)view.findViewById(R.id.spinner1);
		    account2 = (Spinner)view.findViewById(R.id.spinner2);
		    buSave = (Button)view.findViewById(R.id.buDelRow);
		    creatAccount = (Button)view.findViewById(R.id.buCreateAccount);
		    account = (EditText)view.findViewById(R.id.etKontoname);
		    adapter = new AccountListAdapter(this.getActivity(),dbHandler.getAllAccounts());
		    account1.setAdapter(adapter);
		    account2.setAdapter(adapter);
		    buttonEvents();

		    return view;
	}
	
	private void buttonEvents()
	{
			creatAccount.setOnClickListener(new View.OnClickListener()
			{
				@Override
					public void onClick(View view)
					{						
						Log.e("CREATE", "Create Account");
						createAccount(account.getText().toString());
						dbHandler.insertAccount(account.getText().toString(), "accounts");
					}
				});
			buSave.setOnClickListener(new View.OnClickListener()
			{
				@Override
					public void onClick(View view)
					{						
						Log.e("Save", "SAVE WIDGET SETTINGS");
						Account ada = (Account)account1.getSelectedItem();
						Log.e("Account", ada.getName());
						prefs.edit().putInt("widget1",ada.getId()).apply();
						Account ada2 = (Account)account2.getSelectedItem();
						Log.e("Account", ada2.getName());
						prefs.edit().putInt("widget2",ada2.getId()).apply();
						Intent test = new Intent();
						test.setAction("YourAwesomeAction2");
						PendingIntent mPendingIntent = PendingIntent.getBroadcast(view.getContext(), 0, test, 0);
						try {
							mPendingIntent.send();
						} catch (CanceledException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
	}
	
	private void createAccount(String pAccountName)
	{
		Log.e("CREATE", "Create Account: " + pAccountName);
	
	}
}
